
package com.trydofor.a9task.base.sys.id;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 * @date : Nov 26, 2012
 * @see :
 */

public class Id {
    
    /**
     * 30 bits
     * 
     * @param id
     * @return
     */
    public static int int30(int id) {
        return id & 0x3FFFFFFF;
    }
    
    /**
     * 20 bits
     * 
     * @param id
     * @return
     */
    public static int int20(int id) {
        return id & 0xFFFFF;
    }
    
    /**
     * 60 bits
     * 
     * @param id
     * @return
     */
    public static long int60(long id) {
        return id & 0xFFFFFFFFFFFFFFFL;
    }
    
    /**
     * make long id
     * 
     * @param user the user id
     * @param year the year
     * @param table the table seq
     * @param hash the hash
     * @return
     */
    public static long longId(int user, int year, int table, int hash) {
        long h = 0L;
        h |= (int20(user) << 10);
        h |= ((year & 0xF) << 6);
        h |= (table & 0x3F);
        return (h << 30) | int30(hash);
    }
    
    /**
     * get parts [user,year,table,hash] of id
     * 
     * @param id
     * @return
     */
    public static int[] partId(long id) {
        int[] r = new int[4];
        int h = (int) (id >>> 30);
        r[0] = int20(h >>> 10);
        r[1] = (h >>> 6) & 0xF;
        r[2] = h & 0x3F;
        r[3] = int30((int) id);
        return r;
    }
}
